package com.example.ibsp_demo.controller.login;

import com.example.ibsp_demo.pojo.dto.login.LoginDto;
import com.example.ibsp_demo.pojo.dto.login.RegisterDto;
import com.example.ibsp_demo.pojo.dto.login.ResetDto;
import com.example.ibsp_demo.pojo.entity.user.User;
import com.example.ibsp_demo.pojo.response.ResponseMessage;
import com.example.ibsp_demo.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/login")
@CrossOrigin(origins = "http://localhost:8080") // 添加此注解
public class LoginController {

    @Autowired
    private ILoginService iLoginServce;


    //登录就页头两个参数：用户名/手机号/邮箱 + 密码 => get
    @PostMapping
    public ResponseMessage<User> login(@RequestBody LoginDto dto) {
        if(iLoginServce.checkAdmin(dto)){
            return ResponseMessage.error(2025,"管理员登录成功");
        }
        if(iLoginServce.login(dto)){
            User user = iLoginServce.get(dto.getUserInput());
            return ResponseMessage.success(iLoginServce.get(dto.getUserInput()));
        }
        return ResponseMessage.error(2004,"登录失败,用户名/手机号/邮箱或密码错误");
    }

    //注册需要先对 验证码 进行校对，然后传一个完整的 用户数据清洗对象 回来 => post
    @PostMapping("/register")
    public ResponseMessage<User> register(@RequestBody RegisterDto dto) {
        if(iLoginServce.register(dto)){
            return ResponseMessage.success(iLoginServce.get(dto.getEmail()));
        }
        return ResponseMessage.error(2005,"注册失败,用户名/邮箱已被注册");
    }

    //前端应该提示邮箱不能为没有被注册的
    //修改密码 忘记密码需要先对 验证码 进行校对，然后再修改 user 数据 => update
    @PostMapping("/reset")
    public ResponseMessage<User> reset(@RequestBody ResetDto dto) {
        if(iLoginServce.reset(dto)){
            return ResponseMessage.success(iLoginServce.get(dto.getUserInput()));
        }
        return ResponseMessage.error(2006,"重置失败,发生未知错误");
    }

}
